package com.zsyh.jiujiu.fragment;


import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.widget.NestedScrollView;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import com.androidkun.xtablayout.XTabLayout;
import com.blankj.utilcode.util.ActivityUtils;
import com.example.zyh.toolslib.base.BaseFragment;
import com.google.android.material.appbar.AppBarLayout;
import com.google.gson.Gson;
import com.lwb.retrofit.RetrofitTask;
import com.ms.banner.Banner;
import com.ms.banner.listener.OnBannerClickListener;
import com.superluo.textbannerlibrary.ITextBannerItemClickListener;
import com.superluo.textbannerlibrary.TextBannerView;
import com.zsyh.jiujiu.Api;
import com.zsyh.jiujiu.R;
import com.zsyh.jiujiu.bean.BannerBean;
import com.zsyh.jiujiu.bean.ClassOneBean;
import com.zsyh.jiujiu.bean.TouTiaoBean;
import com.zsyh.jiujiu.home.ClassTypeActivity;
import com.zsyh.jiujiu.home.MyvipActtivitty;
import com.zsyh.jiujiu.home.SearchActivity;
import com.zsyh.jiujiu.net.MyFragementObserver;
import com.zsyh.jiujiu.weight.CustomViewHolder;
import com.zsyh.jiujiu.weight.DisInterceptNestedScrollView;
import com.zsyh.jiujiu.weight.NoScrollViewPager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;


public class NewHomeFragment extends BaseFragment {


    @BindView(R.id.uc_zoomiv)
    Banner ucZoomiv;
    @BindView(R.id.reqingku)
    RelativeLayout reqingku;
    @BindView(R.id.huangood)
    RelativeLayout huangood;
    @BindView(R.id.huanservse)
    RelativeLayout huanservse;
    @BindView(R.id.rezhihuan)
    RelativeLayout rezhihuan;
    @BindView(R.id.user_head_container)
    RelativeLayout userHeadContainer;
    @BindView(R.id.govip)
    ImageView govip;
    @BindView(R.id.imagetongao)
    ImageView imagetongao;
    @BindView(R.id.retonggao)
    RelativeLayout retonggao;
    @BindView(R.id.middle_layout)
    DisInterceptNestedScrollView middleLayout;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.title_layout)
    RelativeLayout titleLayout;
    @BindView(R.id.appbar_layout)
    AppBarLayout appbarLayout;
    @BindView(R.id.xTablayout)
    XTabLayout xTablayout;
    @BindView(R.id.scroll)
    NestedScrollView scroll;
    @BindView(R.id.viewpager)
    NoScrollViewPager viewpager;
    @BindView(R.id.container)
    CoordinatorLayout container;
    @BindView(R.id.research)
    RelativeLayout research;
    @BindView(R.id.smartrefresh)
    RelativeLayout smartrefresh;
    @BindView(R.id.tvunbo)
    TextBannerView tvunbo;
    @BindView(R.id.xinqingku)
    TextView xinqingku;
    @BindView(R.id.xinhuangood)
    TextView xinhuangood;
    @BindView(R.id.xinhuanfuwu)
    TextView xinhuanfuwu;
    @BindView(R.id.xinxianzhi)
    TextView xinxianzhi;

    private List<BaseFragment> fragmentList;
    private int selectPos = 0;
    private String[] title;

    @Override
    protected int initLayout() {
        return R.layout.fragment_new_hone;
    }


    @Override
    protected void initView(View rootView) {

    }


    @Override
    protected void initData() {
        fragmentList = new ArrayList<>();
        getdata();
    }


    @OnClick({R.id.xinqingku,R.id.xinhuangood,R.id.xinhuanfuwu,R.id.xinxianzhi,R.id.research, R.id.reqingku, R.id.huangood, R.id.huanservse, R.id.rezhihuan, R.id.govip, R.id.retonggao})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.xinqingku:
                Bundle bundleqing = new Bundle();
                bundleqing.putString("name", "企业清库");
                ActivityUtils.startActivity(bundleqing, ClassTypeActivity.class);
                break;
            case R.id.xinhuangood:
                Bundle bundlehuancp = new Bundle();
                bundlehuancp.putString("name", "换产品");
                ActivityUtils.startActivity(bundlehuancp, ClassTypeActivity.class);
                break;
            case R.id.xinhuanfuwu:
                Bundle bundleservisefw = new Bundle();
                bundleservisefw.putString("name", "换服务");
                ActivityUtils.startActivity(bundleservisefw, ClassTypeActivity.class);

                break;
            case R.id.xinxianzhi:
                Bundle bundlexianxz = new Bundle();
                bundlexianxz.putString("name", "闲置换");
                ActivityUtils.startActivity(bundlexianxz, ClassTypeActivity.class);
                break;
            case R.id.research:
                ActivityUtils.startActivity(SearchActivity.class);
                break;
            case R.id.reqingku:
                Bundle bundle = new Bundle();
                bundle.putString("name", "企业清库");
                ActivityUtils.startActivity(bundle, ClassTypeActivity.class);
                break;
            case R.id.huangood:
                Bundle bundlehuan = new Bundle();
                bundlehuan.putString("name", "换产品");
                ActivityUtils.startActivity(bundlehuan, ClassTypeActivity.class);
                break;
            case R.id.huanservse:
                Bundle bundleservise = new Bundle();
                bundleservise.putString("name", "换服务");
                ActivityUtils.startActivity(bundleservise, ClassTypeActivity.class);

                break;
            case R.id.rezhihuan:
                Bundle bundlexian = new Bundle();
                bundlexian.putString("name", "闲置换");
                ActivityUtils.startActivity(bundlexian, ClassTypeActivity.class);
                break;
            case R.id.govip:
                ActivityUtils.startActivity(MyvipActtivitty.class);
                break;
            case R.id.retonggao:

                break;
        }
    }

    public void getdata() {
        banner();
        Classfenlei("");
        toutiao();
    }


    private class ViewPagerAdapter extends FragmentPagerAdapter {

        private List<BaseFragment> fragmentList;

        public ViewPagerAdapter(FragmentManager fm, List<BaseFragment> fragmentList) {
            super(fm);
            this.fragmentList = fragmentList;
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return title[position];
        }
    }

    /**
     * 获取轮播
     */
    public void banner() {
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .getBanner()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyFragementObserver(getActivity(), false) {
                    @Override
                    protected void onError(String errorMsg) {
                        Log.e("dengluerror", errorMsg + "");
                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Log.e("lunbo", JsonStr + "");
                        Gson gson = new Gson();
                        List<String> listbanner = new ArrayList<>();
                        BannerBean bannerBean = gson.fromJson(JsonStr, BannerBean.class);
                        for (int i = 0; i < bannerBean.getData().size(); i++) {
                            listbanner.add(bannerBean.getData().get(i).getImg());
                        }
                        ucZoomiv.setAutoPlay(true)
                                .setPages(listbanner, new CustomViewHolder())
                                .setDelayTime(3000)
                                .setOnBannerClickListener(new OnBannerClickListener() {
                                    @Override
                                    public void onBannerClick(List datas, int position) {

                                    }
                                })
                                .start();
                    }
                });

    }

    /**
     * 分类
     */
    public void Classfenlei(String pid) {
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .getGoodsClass(pid)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyFragementObserver(getActivity(), false) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Log.e("tixian", JsonStr + "");
                        Gson gson = new Gson();
                        ClassOneBean sucessBean = gson.fromJson(JsonStr, ClassOneBean.class);
                        List<ClassOneBean.DataBean> list = new ArrayList<>();
                        ClassOneBean.DataBean all = new ClassOneBean.DataBean();
                        all.setId("");
                        all.setClassName("全部");
                        list.add(all);
                        for (int i = 0; i < sucessBean.getData().size(); i++) {
                            list.add(sucessBean.getData().get(i));
                        }

                        title = new String[list.size()];
                        for (int i = 0; i < list.size(); i++) {
                            title[i] = list.get(i).getClassName();
                            fragmentList.add(new GoodFragment(list.get(i).getId()));
                        }

                        ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getActivity().getSupportFragmentManager(), fragmentList);
                        viewpager.setOffscreenPageLimit(title.length);
                        viewpager.setAdapter(viewPagerAdapter);
                        xTablayout.setupWithViewPager(viewpager);
                        xTablayout.getTabAt(selectPos).select();
                    }
                });
    }

    /**
     * 头条
     */
    public void toutiao() {
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .getHeadlines()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyFragementObserver(getActivity(), false) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Log.e("toutiao", JsonStr);
                        Gson gson = new Gson();
                        TouTiaoBean touTiaoBean = gson.fromJson(JsonStr, TouTiaoBean.class);
                        List<String> mlist = new ArrayList<>();
                        for (int i = 0; i < touTiaoBean.getData().size(); i++) {
                            mlist.add(touTiaoBean.getData().get(i).getGoodsTitle());
                        }

                        tvunbo.setDatas(mlist);//设置数据
                        tvunbo.setTextDirection(TextBannerView.ALIGN_BOTTOM);//设置滚动形式
                        tvunbo.setItemOnClickListener(new ITextBannerItemClickListener() {
                            @Override
                            public void onItemClick(String data, int position) {

                            }
                        });
                    }
                });
    }
}